<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of tariff_obj
 *
 * @author Juraj
 */
class tariff_obj {
    
    public function getTariffs($what = "*", $filter = array(), $order = '', $limit = '') {

        $db_obj = new db_obj();

        $other_table = '';
        $other_string = '';
        $connector = '';
        if (!empty($order)){
            if (!empty($order['table'])){
                $other_table = ', ' . $order['table'];
                $other_string = "ORDER BY " . $order['table'] . '.' .  $order['orderBy'] . ' ' . $order['order'];
                $connector = 'AND ' . $GLOBALS['tb_tariffs'] . '.' . $order['connector'];
            } else {
                $other_string = "ORDER BY " . $order['orderBy'] . ' ' . $order['order'];
            }
        }
        if ($limit != '')
            $limit = ' LIMIT ' . $limit;
        if (strpos($what, 'COUNT') === false){
            $what = $GLOBALS['tb_tariffs'] . '.' . $what;
        }
        
        $where = 'WHERE 1=1';

        foreach ($filter as $key => $value) {
            $where .= " AND " . $key . "='" . $value . "'";
        }

        $sql = "SELECT $what FROM " . $GLOBALS['tb_tariffs'] . " $other_table $where $connector $other_string $limit";
        base_obj::debug("getAllTariffs sql", $sql);


        return $db_obj->getAll($sql);
    }
    
    public function getTariffsByFulltext($fulltext, $filter = array(), $order = array(), $limit = '', $what = '*') {
        $where = '';
        $db_obj = new db_obj();
        
        $other_table = '';
        $other_string = '';
        $connector = '';
        if (!empty($order)){
            if (!empty($order['table'])){
                $other_table = ', ' . $order['table'];
                $other_string = "ORDER BY " . $order['table'] . '.' .  $order['orderBy'] . ' ' . $order['order'];
                $connector = 'AND ' . $GLOBALS['tb_tariffs'] . '.' . $order['connector'];
            } else {
                $other_string = "ORDER BY " . $order['orderBy'] . ' ' . $order['order'];
            }
        }

        if (!empty($fulltext)) {
            $fulltext = strip_tags(trim($fulltext));

            $fulltext = explode(" ", $fulltext);
            $fulltextResults = array();
            foreach ($fulltext as $key => $word) {
                $sql = "SELECT " . $GLOBALS['tb_tariffs'] . ".Id 
                    FROM " . $GLOBALS['tb_tariffs'] . " 
                    WHERE 
                    " . $GLOBALS['tb_tariffs'] . ".title LIKE '%" . $word . "%' OR 
                    " . $GLOBALS['tb_tariffs'] . ".description LIKE '%" . $word . "%' 
                    ";
                $fulltextResults[] = array_unique($db_obj->getCol($sql));
            }
            if (isset($fulltextResults[0]) && !empty($fulltextResults[0])) {
                $temp = $fulltextResults[0];
                foreach ($fulltextResults as $key => $array) {
                    $temp = array_intersect($temp, $array);
                }
                if (!empty($temp)) {
                    $where .= 'WHERE (';
                    foreach ($temp as $id) {
                        $where .= "Id='" . $id . "' OR ";
                    }
                    $where = substr($where, 0, $length - 4);
                    $where .= ')';
                } else {
                    $where .= 'WHERE 1=2';
                }
            } else {
                $where .= 'WHERE 1=2';
            }
        } else {
            $where .= 'WHERE 1=1';
        }

        if ($limit != '')
            $limit = ' LIMIT ' . $limit;
        if (strpos($what, 'COUNT') === false){
            $what = $GLOBALS['tb_tariffs'] . '.' . $what;
        }

        foreach ($filter as $key => $value) {
            $where .= " AND " . $key . "='" . $value . "'";
        }

        $sql = "SELECT $what FROM " . $GLOBALS['tb_tariffs'] . " $other_table $where $connector $other_string $limit";
        base_obj::debug('getListOfTariffs sql', $sql);

        return $db_obj->getAll($sql);
    }
    
    public function getTariffById($id, $cols = array()) {
        $id = (int) $id;
        if (empty($id))
            return new error_obj('empty tariff id', get_class(), __FUNCTION__);

        $what = '';
        if (!empty($cols)) {
            foreach ($cols as $value) {
                $what .= $value . ',';
            }
            $what = substr($what, 0, strlen($what) - 1);
        } else {
            $what .= '*';
        }

        $db_obj = new db_obj();
        return $db_obj->getRow("SELECT $what FROM " . $GLOBALS['tb_tariffs'] . " WHERE Id=$id");
    }
    
    public function deleteTariffById($id) {
        $id = (int) $id;
        if (empty($id)){
            return new error_obj('deleteTariff -> empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        
        $sql = "DELETE FROM " . $GLOBALS['tb_tariffs'] . " WHERE Id='".$id."'";
        
        $result = $db_obj->query($sql);
        return $result;
    }
    
    public function checkAdd_tariff($check_data) {
        $result = array();
        foreach ($check_data as $key => $value) {
            switch ($key) {
                case 'type_of_record':
                    if (empty($value))
                        $result[] = 'type_of_record';
                    break;
                case 'title':
                    if (empty($value))
                        $result[] = 'title';
                    break;
                case 'unit':
                    if (empty($value))
                        $result[] = 'unit';
                    break;
                case 'servis_tariff':
                    if (!is_numeric($value) && !is_float($value))
                        $result[] = 'servis_tariff';
                    break;
                case 'tariff':
                    if (!is_numeric($value) && !is_float($value))
                        $result[] = 'tariff';
                    break;
            }
        }
        return $result;
    }
    
    public function addTariff($in_data) {
        if (empty($in_data)) {
            return new error_obj('empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->insert($GLOBALS['tb_tariffs'], $in_data);
        if (error_obj::isError($result))
            return $result;
        else
            return $db_obj->lastInsertedId();
    }
    
    public function editTariff($in_data, $id) {
        $id = (int) $id;
        if (empty($id)) {
            return new error_obj('empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $keys = array('Id' => $id);
        return $db_obj->update($GLOBALS['tb_tariffs'], $in_data, $keys);
    }
    
}

?>
